var keyCodes,                     //键位与键盘值对应关系         JSON
    keys,                         //键盘对应的音符              JSON
    rkeys,                        //键盘对应的音符反向           JSON
    reverse,                      //键位与键盘值反向对应关系     JSON
    allScore,                     //所有曲谱                   JSON
    allKeyNum,                    //曲谱键数占位               NUMBER|STRING
    scoreLength,                  //曲谱长度                   NUMBER|STRING
    pianoCodeLength = 500,        //琴码偏移位置设置            NUMBER
    corrections = 30,             //琴谱位置补正                NUMBER
    speedCorre;                   //速度补正                    NUMBER
    

$(function () {
    /**
     * 事件绑定
     */
    $(document).ready(function () {
        let backRandom = parseInt(Math.random() * (5 - 1 + 1) + 1).toString(),
            backnow = 'url("./image/score_background_' + backRandom + '.jpg")';

        Waves.init();
        Waves.attach('.btn');                                    //初始化波浪
        $('#score_back').css("background-image", backnow);       //随机更换背景
        noteInit();                                              //初始化琴键
        getScoreList();                                          //获取曲谱列表
        freePlayInit();                                          //默认初始化自由弹奏

        /**
         * 点击播放
         */
        $("#doplay").click(function () { playstart(); })
        /**
         * 停止
         */
        $("#dostop").click(function () { playstop(); })
        /**
         * 暂停
         */
        $('#dopause').click(function () { playpause(); })
        /**
         * 继续播放
         */
        $("#docontinue").click(function () { playcontinue(); })
        /**
         * 编曲按钮
         */
        $('#addscore').click(function () { layer.msg('编曲模式开发中...', { icon: 1 }) })
        /**
         * 自动演奏按钮
         */
        $('#autoplaybtn').click(function () {
            $('.splayers').css({ "display": "inline" });
            $('.key-wrapper').hide();
            $(this).addClass('btn-active');
            $('#freeplaybtn').removeClass('btn-active');
            $('#trackdetail').show();
            $('#navi .trackhead').show();
            $('#piano_code').show();
            playstop();
            readScore();
            autoInit();
        })
        /**
         * 自由演奏按钮
         */
        $('#freeplaybtn').click(function () {
            $('.splayers').hide();
            $('.key-wrapper').show();
            $(this).addClass('btn-active');
            $('#autoplaybtn').removeClass('btn-active');
            $('#trackdetail').hide();
            $('#navi .trackhead').hide();
            $('#piano_code').hide();
            playstop();
            freePlayInit();
        })

        /**
         * 选择曲谱时触发事件
         */
        $("select[name='scores']").change(function () { readScore(); })

    })

    /**
    * 监听键盘事件
    */
    $(window).keydown(function (event) {
        let nowKey = event.keyCode, keyObj = document.getElementById('code-' + keyCodes[nowKey]);

        if (keyCodes[nowKey] != undefined && keyObj != undefined) {
            if (keyObj.paused) {//是否暂停
                keyObj.play();
            } else {
                //未暂停则设置一个定时删除的播放器
                let nowSrc = keyObj.getAttribute('src'),
                    nowId = 'code-' + randomStr(20),
                    tmpAudio = '<audio id="' + nowId + '"><source src="' + nowSrc + '"></source></audio>';

                $('#audiocontent').append(tmpAudio);
                document.getElementById(nowId).play();
                setTimeout(function () {
                    $('#' + nowId).remove();
                }, 5000)
            }
        }
    })
})

/**
 * 钩子 资源文件
 * @param {*} res 
 */
function hookScore(res) {
    allScore = res;
}
/**
 * 钩子 配置文件
 */
function hookConfig(res) {
    keys = res['ke'];
    rkeys = res['rke'];
    keyCodes = res['k'];
    reverse = res['reverse'];
}

/**
 * 初始化琴板
 */
function noteInit() {
    $('#audiocontent').html('');
    if ($('#freeplaybtn').hasClass('btn_active')) {
        freePlayInit();//自由演奏
    } else {
        //自动演奏
        let audioContent = '<audio>';
    }
}
/**
 * 自由演奏执行方法
 */
function freePlayInit() {
    let tones = $("select[name='tones']").find('option:selected').val(),
        musical = $("select[name='musicals']").find('option:selected').val(),
        audioContent = '<audio>';

    for (let i in keys) {
        let fileName = '';

        fileName += tones + '$' + keys[i] + '$' + i + '.mp3';
        audioContent += '<source id="code-' + i + '" src="./notes/' + musical + '/' + tones + '/' + fileName + '"></source>';
    }
    audioContent += '</audio>';

    $('#audiocontent').html(audioContent);
}


/**
* 读取所有曲谱列表
*/
function getScoreList() {
    for (let i in allScore) {
        $('#scoreslist select').append('<option name="' + i + '" >' + i + '</option>');
    }
}

/**
 * 播放器 用于播放曲谱
 * @param {} keynum 琴键键数
 */
function players(keynum) {

    let i = $("input[name='pauseflag']").val(), speed = $("input[name='speed']").val(), nowScore = allScore[$("select[name='scores']").find('option:selected').val()];

    time();
    function time() {
        if (i <= keynum && $("input[name='stopflag']").val() != '1') {
            let goPlay = [];
            for (let a in nowScore['detail']) {
                if (nowScore['detail'][a][i] != '0' && nowScore['detail'][a][i] != undefined) {
                    let spcr = nowScore['detail'][a][i].split('|'), tmpspcr, tmpspcrtime;

                    goPlay.push(nowScore['instrument'][a] + '-' + nowScore['scale'][a] + '-code-' + keys[spcr[0]]);//当前 主按键

                    //分裂快速弹奏 次按键
                    if (spcr[1] != undefined) {
                        tmpspcr = spcr[1].split(',');
                        tmpspcrtime = spcr[2].split(',');

                        for (let d in tmpspcr) {
                            let tmpPlay = [];
                            tmpPlay.push(nowScore['instrument'][a] + '-' + nowScore['scale'][a] + '-code-' + keys[tmpspcr[d]]);
                            setTimeout(function () {
                                autoGoPlay(tmpPlay);
                            }, tmpspcrtime[d]);
                        }
                    }
                }
            }

            //字符放大
            // $('.note').eq(i - 1).removeClass('noteactive');
            for (let t in nowScore['detail']) {
                $('#trackdetail li').eq(t).find('.note').eq(i).addClass('noteactive');
            }

            $("input[name='pauseflag']").val(i);
            autoGoPlay(goPlay);//
            i++;
            setTimeout(time, speed);
        } else if (i > keynum) {
            // 曲谱播放完毕后
            playstop();
        } else {
            // 未达到条件
            return;
        }
    }
}


/**
 * 读取琴谱数据
 */
function readScore() {
    let n = allScore[$("select[name='scores']").find('option:selected').val()], trackhead = '', tracks = '';

    $('#author').html('曲谱翻译: <span style="color: #F90">' + n['author'] + '</span>');
    $("input[name='speed']").val(n['rate']);
    allKeyNum = n['detail'][0].length;
    scoreLength = allKeyNum * 100;
    speedCorre = n['speedCorre'];

    //接入音轨
    for (let a in n['detail']) {
        trackhead += '<li><div><div>' + (parseInt(a) + 1).toString() + '号音轨</div><div>乐器: <span class="instrument">' + n['instrument'][a] + '</span></div><div>音阶: <span class="scale">' + n['scale'][a] + '</span></div></div></li>';
        tracks += '<li style="width:' + scoreLength + 'px">';
        for (let b in n['detail'][a]) {
            let t = n['detail'][a][b].split('|');
            tracks += '<div class="note">' + t[0] + '</div>';
        }
        tracks += '</li>';
    }

    $('#trackdetail ul').html(tracks);
    $('.trackhead').html(trackhead);
}